package com.example.controller;

import com.example.entity.User;
import com.example.entity.indent;
import com.example.entity.menu;
import com.example.service.IndentService;
import com.example.service.MenuService;
import com.example.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("user")
public class UserController {
      private static final Logger log = LoggerFactory.getLogger(UserController.class);

      private UserService userService;
      private MenuService menuService;
      private IndentService indentService;
      private Integer loginID;

      public UserController(UserService userService, MenuService menuService, IndentService indentService) {
            this.userService = userService;
            this.menuService = menuService;
            this.indentService = indentService;
      }

      @RequestMapping("login")
      public String login(String userName, String userPwd, HttpSession session, Model model) {
            log.debug("接收到的用户名：{}，密码：{}", userName, userPwd);
            try {
                  //            调用业务层，登陆
                  User user = userService.login(userName, userPwd);
                  loginID = user.getId();
//            保存登录信息
                  session.setAttribute("user", user);
                  model.addAttribute("user", user);
                  if (user.getFlag() == 0) {
                        return "redirect:/adminIndex";
                  } else return "userIndex";
            } catch (RuntimeException e) {
                  e.printStackTrace();
                  return "redirect:/index";
            }

      }

      @RequestMapping("userID")
      public String userID(User user, Model model) {

//            自动生成账号且避免重复
            int randomID;
            do {
                  randomID = (int) (900000000 * Math.random() + 100000000);
            } while ((!ObjectUtils.isEmpty(userService.findUserByID(randomID))));
            log.debug("random:{}", randomID);
            user.setId(randomID);
            model.addAttribute("userID", randomID);
            return "userRegister";
      }

      @RequestMapping("register")
      public String register(User user) {
            log.debug("用户名：{},真实姓名：{},密码：{},性别：{}", user.getUserName(), user.getRealName(), user.getUserPwd(), user.getSex());
            userService.register(user);
            loginID = user.getId();
            return "redirect:/index";
      }

      @RequestMapping("findAllMenu")
      public String findAllMenu(Model model) {
            List<menu> menuList = menuService.findAllMenu();
            model.addAttribute("menuLists", menuList);
            return "userMenuList";
      }

      @RequestMapping("addOrder")
      public String addOrder(indent indent, Model model) {

            log.debug("addOrder跳转成功");
            log.debug("供餐方：{}", indent.getMerchantName());
//            log.debug("时间：{}",indent.getTime());
            log.debug("下单方：{}", indent.getUserId());

            String indentTime = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss:SSS").format(new Date());
            indent.setTime(indentTime);

            indentService.addOrder(indent);

            return findAllMenu(model);
      }

      @RequestMapping("findUserIndent")
      public String findUserIndent(Model model, String userId) {

            log.debug("用户ID：{}", loginID);
            log.debug("参数id：{}", userId);

            List<indent> indentOrder = indentService.findUserIndent(userId);

            model.addAttribute("indent", indentOrder);

            return "userIndentList";
      }

      @RequestMapping("payOrder")
      public String payOrder(Integer num, String userId, Model model) {
            log.debug("菜品ID：{}", num);

            indentService.payOrder(num);

            return findUserIndent(model, userId);
      }

      @RequestMapping("userMessage")
      public String userMessage(Integer id, Model model) {
            log.debug("当前登录用户ID：{}", id);

            User user = userService.findUserDetailByID(id);
            model.addAttribute("user",user);

            return "userDetail";
      }

      @RequestMapping("updateUserDetail")
      public String updateUserDetail(User user, Model model) {
            log.debug("当前登录用户ID：{}", user.getId());

            userService.updateUserDetail(user);
            model.addAttribute("user",user);

            return "userDetail";
      }
}
